﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Billing.WindowsBase;
using Billing.DataObjects;

namespace Billing.Win
{
    public partial class AccountNumberPrefixForm : BaseList
    {
        BillingDataContext mBillingDataContext = null;
        public AccountNumberPrefixForm()
        {
            InitializeComponent();
        }
        public AccountNumberPrefixForm(bool Register)
            : base(Register)
        {
            InitializeComponent();
        }

        protected override void onDataLoad()
        {
            mBillingDataContext = new BillingDataContext();
            base.onDataLoad();
            this.bsElement.DataSource = mBillingDataContext.GetTable<AccountNumberPrefix>();
        }
        protected override void onDelete()
        {
            try
            {
                var item = (from n in mBillingDataContext.AccountNumberPrefixes
                            where n.ID == ((AccountNumberPrefix)bsElement.Current).ID
                            select n).First();

                mBillingDataContext.AccountNumberPrefixes.DeleteOnSubmit(item);
                mBillingDataContext.SubmitChanges();
                Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //protected override void onSave()
        //{
        //    base.onSave();
        //    mBillingDataContext.SubmitChanges();
        //}
        protected override void onClose()
        {
            if (AllowSave && (mBillingDataContext.GetChangeSet().Deletes.Count > 0
                || mBillingDataContext.GetChangeSet().Updates.Count > 0
                || mBillingDataContext.GetChangeSet().Inserts.Count > 0))
            {
                if (MessageBox.Show("შევინახო შეცვლილი მონაცემები?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    mBillingDataContext.SubmitChanges();
            }
            base.onClose();
        }
        protected override void onEditorOpen(eOpenMode OpenMode)
        {
            base.onEditorOpen(OpenMode);
            AccountNumberPrefixEditForm form;
            if (OpenMode == Billing.WindowsBase.eOpenMode.New)
            {
                form = new AccountNumberPrefixEditForm(OpenMode, 0);
            }
            else
            {
                form = new AccountNumberPrefixEditForm(OpenMode, ((AccountNumberPrefix)bsElement.Current).ID);
            }
            if (form.ShowDialog() == DialogResult.OK)
                Refresh();
        }
        protected override void onElementInitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            base.onElementInitializeLayout(sender, e);
            grd.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.RowSelect;
            grd.DisplayLayout.GroupByBox.Hidden = false;

            if (e.Layout.Bands[0].Columns.Count > 0)
            {
                e.Layout.Bands[0].Columns["ID"].Hidden = true;
                e.Layout.Bands[0].Columns["CreatedDate"].Hidden = true;
                e.Layout.Bands[0].Columns["ModifiedDate"].Hidden = true;
                e.Layout.Bands[0].Columns["RowVersion"].Hidden = true;
                e.Layout.Bands[0].Columns["AccountNumberPrefixGroupID"].Hidden = true;

                e.Layout.Bands[0].Columns["Prefix"].Header.Caption = "პრეფიქსი";
                e.Layout.Bands[0].Columns["AccountNumberPrefixGroup"].Header.Caption = "ჯგუფი";
                e.Layout.Bands[0].Columns["AccountNumberPrefixGroup"].Header.VisiblePosition = 0;
                e.Layout.Bands[0].Columns["AccountNumberPrefixGroup"].Width = 200;
                e.Layout.Bands[0].SortedColumns.Add("AccountNumberPrefixGroup", false, true);
                e.Layout.Bands[0].Columns["Length"].Header.Caption = "სიგრძე";
            }
        }
    }
}
